Migo商城2.0 门户首页大广告位显示(4) 十八

Migo商城2.0 门户首页大广告位显示(4) 十八

关于httpclient的东西就不说了,不懂的可以百度谷歌的

common工程中导入httpclient工具类

首页通过EL表达式输出到页面显示:

前台系统service代码:

此处代码和1版做了很大修改,之前的migoResult在做转换的时候出了点问题,索性就自己重新再写下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package com.migo.portal.service;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.migo.portal.pojo.Content;
import com.migo.utils.HttpClientUtil;
import com.migo.utils.JsonUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
* Author 知秋
* Created by kauw on 2016/12/4.
*/
@Service
public class IndexService {

@Value("${MIGO_MANAGE_URL}")
private String MIGO_MANAGE_URL;

@Value("${AD1_URL}")
private String AD1_URL;

private static final ObjectMapper MAPPER = new ObjectMapper();

public String queryAD1(){
try {
//调用后台管理系统的接口服务获取数据
String url=MIGO_MANAGE_URL+AD1_URL;
String jsonData = HttpClientUtil.doGet(url);
if (jsonData==null){
return null;
}
//解析json数据
JsonNode jsonNode = MAPPER.readTree(jsonData);
ArrayNode rows = (ArrayNode) jsonNode.get("rows");
List<Content> contents = JsonUtils.jsonToList(String.valueOf(rows), Content.class);
List<Map<String,Object>> result1=new ArrayList<>();
for (Content content : contents) {
Map<String,Object> map=new LinkedHashMap<>();
map.put("srcB",content.getPic());
map.put("height",240);
map.put("alt",content.getTitle());
map.put("width",670);
map.put("src",content.getPic2());
map.put("widthB",550);
map.put("href",content.getUrl());
map.put("heightB",240);
result1.add(map);
}

return JsonUtils.objectToJson(result1);
} catch (Exception e) {
e.printStackTrace();
}
return null;


}
}

controller代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package com.migo.portal.controller;

import com.migo.portal.service.IndexService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
* Author 知秋
* Created by kauw on 2016/11/16.
*/
@Controller
public class IndexController {
@Autowired
private IndexService indexService;
/**
* 首页
*/
@RequestMapping(value = "index",method=RequestMethod.GET)
public String index(Model model){

String ad1 = this.indexService.queryAD1();
//传递给页面
model.addAttribute("indexAd1",ad1);

return "index";
}
}

运行测试:

其他的广告位就暂时不写了,都一个套路,后台写个服务,前台系统查下,然后写到model里面,最后也没调用el表达式完成,至此,大广告位显示完成

您的支持将鼓励我继续创作!